import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { MallOrderApi } from '#/api/mall/trade/order';
import type { DescriptionItemSchema } from '#/components/description';

import { h } from 'vue';

import { DICT_TYPE } from '@vben/constants';
import { fenToYuan, formatDateTime } from '@vben/utils';

import { DictTag } from '#/components/dict-tag';

import { $t } from '#/locales';

/** 订单基础信息 schema */
export function useOrderInfoSchema(): DescriptionItemSchema[] {
  return [
    {
      field: 'no',
      label: $t('views.mall.trade.order.no'),
    },
    {
      field: 'user.nickname',
      label: $t('views.mall.trade.order.user'),
    },
    {
      field: 'type',
      label: $t('views.mall.trade.order.type'),
      content: (data: MallOrderApi.Order) =>
        h(DictTag, {
          type: DICT_TYPE.TRADE_ORDER_TYPE,
          value: data?.type,
        }),
    },
    {
      field: 'terminal',
      label: $t('views.mall.trade.order.terminal'),
      content: (data: MallOrderApi.Order) =>
        h(DictTag, {
          type: DICT_TYPE.TERMINAL,
          value: data?.terminal,
        }),
    },
    {
      field: 'userRemark',
      label: $t('views.mall.trade.order.userRemark'),
    },
    {
      field: 'remark',
      label: $t('views.mall.trade.order.remark'),
    },
    {
      field: 'payOrderId',
      label: $t('views.mall.trade.order.payOrderId'),
    },
    {
      field: 'payChannelCode',
      label: $t('views.mall.trade.order.payChannelCode'),
      content: (data: MallOrderApi.Order) =>
        h(DictTag, {
          type: DICT_TYPE.PAY_CHANNEL_CODE,
          value: data?.payChannelCode,
        }),
    },
    {
      field: 'brokerageUser.nickname',
      label: $t('views.mall.trade.order.brokerageUser'),
    },
  ];
}

/** 订单状态信息 schema */
export function useOrderStatusSchema(): DescriptionItemSchema[] {
  return [
    {
      field: 'status',
      label: $t('views.mall.trade.order.status'),
      content: (data: MallOrderApi.Order) =>
        h(DictTag, {
          type: DICT_TYPE.TRADE_ORDER_STATUS,
          value: data?.status,
        }),
    },
    {
      field: 'reminder',
      label: $t('views.mall.trade.order.reminder'),
      content: () =>
        h('div', { class: 'space-y-1' }, [
          h('div', $t('views.mall.trade.order.detail.reminderContent-1')),
          h('div', $t('views.mall.trade.order.detail.reminderContent-2')),
          h(
            'div',
            $t('views.mall.trade.order.detail.reminderContent-3'),
          ),
        ]),
    },
  ];
}

/** 订单金额信息 schema */
export function useOrderPriceSchema(): DescriptionItemSchema[] {
  return [
    {
      field: 'totalPrice',
      label: $t('views.mall.trade.order.detail.priceSchema.totalPrice'),
      content: (data: MallOrderApi.Order) =>
        `${fenToYuan(data?.totalPrice ?? 0)} 元`,
    },
    {
      field: 'deliveryPrice',
      label: $t('views.mall.trade.order.detail.priceSchema.deliveryPrice'),
      content: (data: MallOrderApi.Order) =>
        `${fenToYuan(data?.deliveryPrice ?? 0)} 元`,
    },
    {
      field: 'adjustPrice',
      label: $t('views.mall.trade.order.detail.priceSchema.adjustPrice'),
      content: (data: MallOrderApi.Order) =>
        `${fenToYuan(data?.adjustPrice ?? 0)} 元`,
    },
    {
      field: 'couponPrice',
      label: $t('views.mall.trade.order.detail.priceSchema.couponPrice'),
      content: (data: MallOrderApi.Order) =>
        h(
          'span',
          { class: 'text-red-500' },
          `${fenToYuan(data?.couponPrice ?? 0)} 元`,
        ),
    },
    {
      field: 'vipPrice',
      label: $t('views.mall.trade.order.detail.priceSchema.vipPrice'),
      content: (data: MallOrderApi.Order) =>
        h(
          'span',
          { class: 'text-red-500' },
          `${fenToYuan(data?.vipPrice ?? 0)} 元`,
        ),
    },
    {
      field: 'discountPrice',
      label: $t('views.mall.trade.order.detail.priceSchema.discountPrice'),
      content: (data: MallOrderApi.Order) =>
        h(
          'span',
          { class: 'text-red-500' },
          `${fenToYuan(data?.discountPrice ?? 0)} 元`,
        ),
    },
    {
      field: 'pointPrice',
      label: $t('views.mall.trade.order.detail.priceSchema.pointPrice'),
      content: (data: MallOrderApi.Order) =>
        h(
          'span',
          { class: 'text-red-500' },
          `${fenToYuan(data?.pointPrice ?? 0)} 元`,
        ),
    },
    {
      field: 'payPrice',
      label: $t('views.mall.trade.order.detail.priceSchema.payPrice'),
      content: (data: MallOrderApi.Order) =>
        `${fenToYuan(data?.payPrice ?? 0)} 元`,
    },
  ];
}

/** 收货信息 schema */
export function useDeliveryInfoSchema(): DescriptionItemSchema[] {
  return [
    {
      field: 'deliveryType',
      label: $t('views.mall.trade.order.deliveryType'),
      content: (data: MallOrderApi.Order) =>
        h(DictTag, {
          type: DICT_TYPE.TRADE_DELIVERY_TYPE,
          value: data?.deliveryType,
        }),
    },
    {
      field: 'receiverName',
      label: $t('views.mall.trade.order.detail.deliverySchema.receiverName'),
    },
    {
      field: 'receiverMobile',
      label: $t('views.mall.trade.order.detail.deliverySchema.receiverMobile'),
    },
    {
      field: 'receiverAddress',
      label: $t('views.mall.trade.order.detail.deliverySchema.receiverAddress'),
      content: (data: MallOrderApi.Order) =>
        `${data?.receiverAreaName} ${data?.receiverDetailAddress}`.trim(),
    },
    {
      field: 'deliveryTime',
      label: $t('views.mall.trade.order.detail.deliverySchema.deliveryTime'),
      content: (data: MallOrderApi.Order) =>
        formatDateTime(data?.deliveryTime) as string,
    },
  ];
}

/** 商品信息 columns */
export function useProductColumns(): VxeTableGridOptions['columns'] {
  return [
    {
      field: 'spuName',
      title: $t('views.mall.trade.order.detail.productSchema.spuName'),
      minWidth: 300,
      slots: { default: 'spuName' },
    },
    {
      field: 'price',
      title: $t('views.mall.trade.order.detail.productSchema.price'),
      width: 150,
      formatter: 'formatFenToYuanAmount',
    },
    {
      field: 'count',
      title: $t('views.mall.trade.order.detail.productSchema.count'),
      width: 100,
    },
    {
      field: 'payPrice',
      title: $t('views.mall.trade.order.detail.productSchema.payPrice'),
      width: 150,
      formatter: 'formatFenToYuanAmount',
    },
    {
      field: 'afterSaleStatus',
      title: $t('views.mall.trade.order.detail.productSchema.afterSaleStatus'),
      width: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.TRADE_ORDER_ITEM_AFTER_SALE_STATUS },
      },
    },
  ];
}

/** 物流详情 columns */
export function useExpressTrackColumns(): VxeTableGridOptions['columns'] {
  return [
    {
      field: 'time',
      title: $t('views.mall.trade.order.detail.expressTrackSchema.time'),
      width: 180,
      formatter: 'formatDateTime',
    },
    {
      field: 'content',
      title: $t('views.mall.trade.order.detail.expressTrackSchema.content'),
      minWidth: 300,
    },
  ];
}

/** 操作日志 columns */
export function useOperateLogColumns(): VxeTableGridOptions['columns'] {
  return [
    {
      field: 'createTime',
      title: $t('views.mall.trade.order.detail.operateLogSchema.createTime'),
      width: 180,
      formatter: 'formatDateTime',
    },
    {
      field: 'userType',
      title: $t('views.mall.trade.order.detail.operateLogSchema.userType'),
      width: 100,
      slots: { default: 'userType' },
    },
    {
      field: 'content',
      title: $t('views.mall.trade.order.detail.operateLogSchema.content'),
      minWidth: 200,
    },
  ];
}
